import json
from PyQt5 import QtCore
import time


class GaodeHtmlRender(QtCore.QThread):
    result_signal = QtCore.pyqtSignal(str)
    progress_signal = QtCore.pyqtSignal(str)
    error_signal = QtCore.pyqtSignal(str)
    finished_signal = QtCore.pyqtSignal(str)

    def __init__(self, points_str):
        super(GaodeHtmlRender, self).__init__()
        self.points_str = points_str

    def read_local_json(self):
        with open("settings.json", 'r', encoding='utf8')as f:
            settings_json = json.load(f)
        return settings_json

    def run(self):
        self.progress_signal.emit("开始加载html")
        import time
        time.sleep(2)
        points = eval(self.points_str)
        settings_json = self.read_local_json()
        points_for_gd = [[point['lon'], point['lat']] for point in points]

        html_str = settings_json.get("gd_html_str")
        html_str = html_str.replace("points_data[0]", str(points_for_gd[0]))
        html_str = html_str.replace("points_data", str(points_for_gd))

        # print(points)
        self.result_signal.emit(html_str)

        self.progress_signal.emit("疯狂加载中！，请耐心等待")
        time.sleep(1.5)
        self.progress_signal.emit("加载地图数据完毕，请确保当前网络畅通！")
        time.sleep(3)
        self.progress_signal.emit("done")

